/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author Sonnavy
 */
public class DaftarMemberTable {
    

    public DaftarMemberTable() {
       emf = Persistence.createEntityManagerFactory("persistence");
       
    }
    private  EntityManagerFactory emf = null;
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(DaftarMember daftarMember) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(daftarMember);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(DaftarMember daftarMember) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            daftarMember = em.merge(daftarMember);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = daftarMember.getId();
                
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DaftarMember daftarMember;
            try {
                daftarMember = em.getReference(DaftarMember.class, id);
                daftarMember.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The daftarMember with id " + id + " no longer exists.", enfe);
            }
            em.remove(daftarMember);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
     public List<DaftarMember> getDaftarMember() {
        List<DaftarMember> users = new ArrayList<DaftarMember>();
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM DaftarMember AS o ");
            users = q.getResultList();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return users;
    }

    public DaftarMember getuser (String nama) {
        DaftarMember user = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM DaftarMember AS o WHERE o.username=:usr");
            q.setParameter("usr", nama);
            user = (DaftarMember) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return user;
    }

    public boolean check(String nama) {
        boolean result = false;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT count(o) FROM DaftarMember AS o WHERE o.username=:usr");
            q.setParameter("usr", nama);
            int jumlahUser = ((Integer) q.getSingleResult()).intValue();
            if (jumlahUser > 0) {
                result = true;
            }
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }
}
